//
//  IssueVC.m
//  YaoHeAPP
//
//  Created by mac on 15/11/20.
//  Copyright (c) 2015年 mac. All rights reserved.

//************************* 发布求购页面

#import "IssueVC.h"
#import "AccurateVC.h"
#import "Masonry.h"
#import "TakingPicturesVC.h"
#import "CreateCompanyVC.h"
#import "Tool.h"
#import "Controller.h"
#import "FindCompanyViewController.h"
#import "CompanyInviteCodeViewController.h"
#import "YongHuQuanXuanModel.h"
#import "MJExtension.h"
#import "TongYong.h"
@interface IssueVC ()
{
    NSInteger width;
    NSInteger height;
    UIButton*button;
}
@end

@implementation IssueVC

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"IssueVC"];
}


//判断是否注册过公司
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"IssueVC"];
    
    //判断是否有创建公司的权限
    [self panduanCreate];
    
}

//判断是否创建公司
-(void)panduanCreate
{
    [Controller requestWithURL:POSTCHECK jsondic:nil finishBlock:^(ClassData *data) {
        if ([data.number isEqualToNumber:[NSNumber numberWithInteger:0]]){
            YongHuQuanXuanModel *yongHuQuanXuanModel=[YongHuQuanXuanModel mj_objectWithKeyValues:data.data];
            NSString *IDnumString=[NSString stringWithFormat:@"%@",yongHuQuanXuanModel.COMPANY_ID];
            if ([IDnumString isEqualToString:@"0"]){
                NSString *statusSt=[NSString stringWithFormat:@"%@",yongHuQuanXuanModel.STATUS];
                if ([TongYong isBlankObjectAndStr:yongHuQuanXuanModel.STATUS]||[statusSt isEqualToString:@"-1"]) {
                    //没有值便是 申请
                    //没有绑定公司 （提示是否加入公司  或者  创建公司  或者 输入验证码 加入公司）
                    
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                                    message:@"您还没有“创建/加入”一家公司，不能进行该操作"
                                                                   delegate:self
                                                          cancelButtonTitle:@"取消"
                                                          otherButtonTitles:@"创建公司",@"查找公司",@"输入邀请码",nil];
                    
                    //这个属性继承自UIView，当一个视图中有多个AlertView时，可以用这个属性来区分
                    alert.tag = 0;
                    //只读属性，看AlertView是否可见
                    [alert show];
                }else{
                    //0申请中  -1被拒绝
                    NSString *messageSt=[NSString stringWithFormat:@"您于%@申请加入“%@”，公司主管理员尚未同意您加入公司。",yongHuQuanXuanModel.APPLY_TIME,yongHuQuanXuanModel.APPLY_COMPANY_NAME];
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                                    message:messageSt
                                                                   delegate:self
                                                          cancelButtonTitle:@"取消申请"
                                                          otherButtonTitles:@"关闭",nil];
                    
                    //这个属性继承自UIView，当一个视图中有多个AlertView时，可以用这个属性来区分
                    alert.tag = 100;
                    //只读属性，看AlertView是否可见
                    [alert show];
                    
                }
        }else{
            
        }
    }
    }];
    
}

//根据被点击按钮的索引处理点击事件
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    if (alertView.tag==0) {
        if(buttonIndex==0){
            //取消
            RootTabBarViewController *rootTabBarViewController = [RootTabBarViewController rootViewController];
            [rootTabBarViewController changeRootViewControllerTo:0];
            
        }else if (buttonIndex==1) {
            //创建公司
            CreateCompanyVC*creatVC=[CreateCompanyVC new];
            [self.navigationController pushViewController:creatVC animated:YES];
            
        }else if(buttonIndex==2){
            //查找公司
            FindCompanyViewController*findCompanyViewController=[FindCompanyViewController new];
            [self.navigationController pushViewController:findCompanyViewController animated:NO];
            
        }else if (buttonIndex==3){
            //输入邀请码
            CompanyInviteCodeViewController *companyInviteCodeViewController=[CompanyInviteCodeViewController new];
            [self.navigationController pushViewController:companyInviteCodeViewController animated:YES];
        }
        
    }else if(alertView.tag==100){
        if (buttonIndex==0) {
            //取消申请
            [self quxiaoshenqingBut];
        }else{
            //取消
            RootTabBarViewController *rootTabBarViewController = [RootTabBarViewController rootViewController];
            [rootTabBarViewController changeRootViewControllerTo:0];
        }
        
    }
}
//取消加入公司申请
-(void)quxiaoshenqingBut{
    
    [Controller requestWithURL:PostCancelApply jsondic:nil finishBlock:^(ClassData *data) {
        if ([data.number isEqualToNumber:[NSNumber numberWithInteger:0]]){
            ALERTSHOW(@"你已成功取消加入该公司的申请")
        }else{
            
            ALERTSHOW(data.message);
        }
    }];
    
}

- (void)willPresentAlertView:(UIAlertView *)alertView{
    
    for( UIView * view in alertView.subviews ){
        
        if( [view isKindOfClass:[UILabel class]] )
        {
            UILabel* label = (UILabel*) view;
            
            label.textAlignment = NSTextAlignmentLeft;
        }
        
    }
}


- (void)viewDidLoad {
    [super viewDidLoad];
    width=self.view.frame.size.width;
    height=self.view.frame.size.height;
    //隐藏导航条
    self.navigationController.navigationBar.hidden=YES;
    
    self.view.backgroundColor=bgVCcolor;
    //加一个导航条
    UIView*navcBar=[[UIView alloc]init];
    navcBar.backgroundColor=[UIColor colorWithRed:90.f/255.f green:191.f/255.f blue:219.f/255.f alpha:1];
    [self.view addSubview:navcBar];
    [self.view bringSubviewToFront:navcBar];
    [navcBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(0);   make.left.offset(0);
        make.height.offset(kNavigationBarHeight);  make.right.offset(0);
    }];
    //导航条标题
    UILabel*titleLabel=[[UILabel alloc]init];
    titleLabel.text=@"发布求购";
    titleLabel.font=Font(17);
    titleLabel.textAlignment=NSTextAlignmentCenter;
    titleLabel.textColor=[UIColor whiteColor];
    [navcBar addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(25);
        make.left.offset(width/2-50);
        make.width.offset(100);
        make.height.offset(30);
    }];

    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"bg_user_center"]forBarMetrics:UIBarMetricsDefault];
    self.navigationItem.title=@"发布求购";
    //初始化表格
    _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, kNavigationBarHeight, width, height-kNavigationBarHeight-kTabBarHeight) style:UITableViewStylePlain];
    _tableView.backgroundColor= [UIColor colorWithRed:230/255.0 green:232/255.0 blue:241/255.0  alpha:1];
    _tableView.delegate=self;
    _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    _tableView.dataSource=self;
    [self.view addSubview:_tableView];

}

#pragma mark--------表格的协议方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;//返回两个分区
}
//每个分区返回的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section==0)
    {
        return 1;
    }
    return 2;
}
//每一行单元格的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0)
    {
        return width/1.3;
    }
    return 60;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UITableViewHeaderFooterView*headerView=[[UITableViewHeaderFooterView alloc]init];
    //标签
    UILabel*labelColor=[[UILabel alloc]init];
    labelColor.frame=CGRectMake(15, 12, 6, 16);
    labelColor.backgroundColor=[UIColor orangeColor];
    [headerView addSubview:labelColor];
    
        //标签
    UILabel*labelTxt=[[UILabel alloc]init];
    if (section==0) {
       labelTxt.text=@"精确找货";
    }
    if(section==1){
       labelTxt.text=@"快捷找货";
    }
    labelTxt.frame=CGRectMake(25, 10, 100, 20);
    labelTxt.font=[UIFont systemFontOfSize:15];
    labelTxt.textAlignment=NSTextAlignmentLeft;
    labelTxt.textColor=[UIColor blackColor];
    [headerView addSubview:labelTxt];
    return headerView;
}

//展示要显示的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //*********精确找货
    if (indexPath.section==0)
    {
        UITableViewCell*oneCell=[[UITableViewCell alloc]init];
        oneCell.selectionStyle=UITableViewCellSelectionStyleNone;
        NSArray*imageArray1=@[@"gangjuan",@"gangban",@"guancai",@"xincai",@"lvcai"];
        NSArray*imageArray2=@[@"gangjuan_hl",@"gangban_hl",@"guancai_hl",@"xincai_hl",@"lvcai_hl"];
        NSArray*titleArray=@[@"钢卷",@"钢板",@"钢管",@"型材",@"铝材"];
        NSInteger  cellHeight=width/1.3;
        for (NSInteger i=0; i<3; i++)
        {
            //创建3个按钮
            button=[UIButton buttonWithType:UIButtonTypeCustom];
            button.frame=CGRectMake(width/5*2/6+(width/2*0.4+width/5*2/3)*i, 20, width/2*0.4, width/2*0.4);
            [button setBackgroundImage:[UIImage imageNamed:imageArray1[i]] forState:UIControlStateNormal];
            [button setBackgroundImage:[UIImage imageNamed:imageArray2[i]] forState:UIControlStateHighlighted];
            button.tag=i+100;
            [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
            [oneCell addSubview:button];
            //按钮下方要显示的文字
            UILabel*label=[[UILabel alloc]init];
            label.font=[UIFont systemFontOfSize:15];
            label.text=titleArray[i];
            label.frame=CGRectMake(width/5*2/6+(width/2*0.4+width/5*2/3)*i,(20+width/2*0.4)+((cellHeight/2-(20+width/2*0.4))/2-10), width/2*0.4,20);
            label.textColor=[UIColor grayColor];
            label.textAlignment=NSTextAlignmentCenter;
            [oneCell addSubview:label];
            //水平方向
            UIImageView*Himage=[[UIImageView alloc]initWithFrame:CGRectMake(width/20*1, cellHeight/2-0.5, width-width/20*2,0.5)];
            Himage.backgroundColor=[UIColor grayColor];
            Himage.alpha=0.5;
            [oneCell addSubview:Himage];
        }
        for (NSInteger a=3; a<5; a++) {
            button=[UIButton buttonWithType:UIButtonTypeCustom];
            button.frame=CGRectMake(width/20*3+width/2*(a%3), 20+cellHeight/2, width/2*0.4, width/2*0.4);
            [button setBackgroundImage:[UIImage imageNamed:imageArray1[a]] forState:UIControlStateNormal];
            [button setBackgroundImage:[UIImage imageNamed:imageArray2[a]] forState:UIControlStateHighlighted];
            button.tag=a+100;
            [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
            [oneCell addSubview:button];
            //按钮下方要显示的文字
            UILabel*label=[[UILabel alloc]init];
            label.font=[UIFont systemFontOfSize:15];
            label.text=titleArray[a];
            label.frame=CGRectMake(width/20*3+width/2*(a%3),(cellHeight/2+(20+width/2*0.4))+((cellHeight/2-(20+width/2*0.4))/2-10),width/2*0.4,20);
            label.textColor=[UIColor grayColor];
            label.textAlignment=NSTextAlignmentCenter;
            [oneCell addSubview:label];
        }
        return oneCell;
    }
    
    //***********快捷找货
    UITableViewCell*twoCell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
    twoCell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    twoCell.selectionStyle=UITableViewCellSelectionStyleNone;
    if (indexPath.row==0)
    {
        UIImageView*phoneImage=[[UIImageView alloc]init];
        phoneImage.image=[UIImage imageNamed:@"ic_zh_camera"];
        phoneImage.frame=CGRectMake(18, 15, 30, 30);
        [twoCell addSubview:phoneImage];
        
        UILabel*label1=[[UILabel alloc]init];
        label1.text=@"拍照找货";
        label1.font=[UIFont systemFontOfSize:15];
        [twoCell addSubview:label1];
        [label1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(phoneImage.mas_right).offset(10);
            make.top.offset(10);  make.height.offset(15);
            make.right.offset(-5);
        }];
        
        
        UILabel*label2=[[UILabel alloc]init];
        label2.text=@"用手机拍下采购的货品上传即可";
        label2.font=[UIFont systemFontOfSize:14];
        label2.textColor=[UIColor grayColor];
        [twoCell addSubview:label2];
        [label2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(phoneImage.mas_right).offset(10);
            make.top.offset(35);  make.height.offset(15);
            make.right.offset(-5);
        }];

    
        /*
        twoCell.imageView.image=[UIImage imageNamed:@"ic_zh_camera"];
        twoCell.textLabel.text=@"拍照找货";
        twoCell.detailTextLabel.text=@"用手机拍下采购的货品上传即可";*/
    }
    if (indexPath.row==1)
    {
        
        UIImageView*phoneImage=[[UIImageView alloc]init];
        phoneImage.image=[UIImage imageNamed:@"ic_zh_phone"];
        phoneImage.frame=CGRectMake(18, 15, 30, 30);
        [twoCell addSubview:phoneImage];
        
        UILabel*label1=[[UILabel alloc]init];
        label1.text=@"电话找货";
        label1.font=[UIFont systemFontOfSize:15];
        [twoCell addSubview:label1];
        [label1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(phoneImage.mas_right).offset(10);
            make.top.offset(10);  make.height.offset(15);
            make.right.offset(-5);
        }];
        
        UILabel*label2=[[UILabel alloc]init];
        label2.text=@"呼叫客服,协助找货";
        label2.font=[UIFont systemFontOfSize:14];
        label2.textColor=[UIColor grayColor];
        [twoCell addSubview:label2];
        [label2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(phoneImage.mas_right).offset(10);
            make.top.offset(35);  make.height.offset(15);
            make.right.offset(-5);
        }];
        /*twoCell.imageView.image=[UIImage imageNamed:@"ic_zh_phone"];
        twoCell.textLabel.text=@"电话找货";
        twoCell.detailTextLabel.text=@"呼叫客服,协助找货";*/
    }
    return twoCell;
}

//返回分区区头的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}
//选中单元格时执行的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==1)
    {
        if (indexPath.row==0)
        {
            //进入拍照页面
            
            [MobClick event:@"findTheGoodsPhotographed"];
            
            TakingPicturesVC*takePicVC=[[TakingPicturesVC alloc]init];
            [self.navigationController pushViewController:takePicVC animated:YES];
            
        }
        else
        {
            [MobClick event:@"phoneToFindGoods"];
            //进入打电话页面
            UIWebView *webview = [[UIWebView alloc] init];
            [webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"tel:4008850051"]]];
            [self.view addSubview:webview];
            //[[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"tel://10086"]];
        }
            
            
    }
}
//点击不同的按钮进入不同的页面
-(void)buttonClick:(UIButton*)btn
{
    NSInteger tag=btn.tag;
    AccurateVC*accyate=[AccurateVC new];
    switch (tag){
        case  100:
        {
            [MobClick event:@"getGangjuan"];
            accyate.typeString=@"卷材";
            accyate.pageString=@"直接采购";
            [self.navigationController pushViewController:accyate animated:YES];
        }
        break;
        case  101:
        {
            [MobClick event:@"getGangban"];
            accyate.typeString=@"板材";
            accyate.pageString=@"直接采购";
            [self.navigationController pushViewController:accyate animated:YES];
        }
            break;
        case  102:
        {
            [MobClick event:@"getGangguan"];
            accyate.typeString=@"管材";
            accyate.pageString=@"直接采购";
            [self.navigationController pushViewController:accyate animated:YES];
        }
            break;
        case  103:
        {
            [MobClick event:@"getXingcai"];
            accyate.typeString=@"型材";
            accyate.pageString=@"直接采购";
            [self.navigationController pushViewController:accyate animated:YES];
        }
            break;
        case  104:
        {
            [MobClick event:@"getLvcai"];
            accyate.typeString=@"铝材";
            accyate.pageString=@"直接采购";
            [self.navigationController pushViewController:accyate animated:YES];
        }
            break;
        default:
            break;
    }
}

@end
